//
//  AmbitusAPPViewController.m
//  Commerce
//
//  Created by gc on 15/3/18.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "AmbitusAPPViewController.h"
#import <BaiduMapAPI_Search/BMKSearchComponent.h>

@interface AmbitusAPPViewController ()<BMKPoiSearchDelegate,BMKMapViewDelegate>{
    BMKMapView* _mapView;
    BMKPoiSearch *_searcher;
    NSMutableArray *_annotationList;
    BMKNearbySearchOption *_option;
}

@end

@implementation AmbitusAPPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, self.navView.frame.origin.y + self.navView.frame.size.height , 320, self.view.frame.size.height - self.navView.frame.size.height-40)];
    [self.view addSubview:_mapView];
    
    //初始化检索对象
    _searcher =[[BMKPoiSearch alloc]init];
    _searcher.delegate = self;
    //发起检索
    _option = [[BMKNearbySearchOption alloc]init];
    _option.pageIndex = 1;
    _option.pageCapacity = 20;
    _option.location = [[GlobalData sharedGlobalData] uselocation];
    _option.keyword = @"饭店";
    
    BOOL flag = [_searcher poiSearchNearBy:_option];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
    }
    else
    {
        NSLog(@"周边检索发送失败");
    }

    _annotationList = [[NSMutableArray alloc] init];

    // Do any additional setup after loading the view from its nib.
}

-(IBAction)searchKeyword:(id)sender{
    NSLog(@"%@",[[(UIButton *)sender titleLabel] text]);
    _option.keyword = [[(UIButton *)sender titleLabel] text];

    BOOL flag = [_searcher poiSearchNearBy:_option];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
    }
    else
    {
        NSLog(@"周边检索发送失败");
    }

}

-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
//    _mapView.showsUserLocation = YES;//开启定位服务
//    BMKCoordinateRegion region = BMKCoordinateRegionMake([[GlobalData sharedGlobalData] uselocation], BMKCoordinateSpanMake(0.03, 0.03));
//    [_mapView setRegion:region animated:NO];
    [_mapView updateLocationData:[[GlobalData sharedGlobalData] bUserLocation]];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    _searcher.delegate = nil;
}

//实现PoiSearchDeleage处理回调结果
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
//        [_annotationList removeAllObjects];
//        [_annotationList addObjectsFromArray:poiResultList.poiInfoList];
        [self setAnnotionsWithList:poiResultList.poiInfoList];
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果，但在其他城市找到结果时，回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
    } else {
        NSLog(@"抱歉，未找到结果");
    }
}

-(void)setAnnotionsWithList:(NSArray *)list
{
    for (BMKPoiInfo *poiInfo in list) {
        
        
        BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc]init];

        pointAnnotation.coordinate = poiInfo.pt;
        pointAnnotation.title = poiInfo.name;
        pointAnnotation.subtitle = [NSString stringWithFormat:@"%@",poiInfo.address];
        [_mapView addAnnotation:pointAnnotation];
    }
}

#pragma mark -
#pragma mark implement BMKMapViewDelegate


// 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    //普通annotation
    NSString *AnnotationViewID = @"renameMark";
    BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        // 设置颜色
        annotationView.pinColor = BMKPinAnnotationColorPurple;
        // 从天上掉下效果
        annotationView.animatesDrop = YES;
        // 设置可拖拽
        annotationView.draggable = NO;
    }
    return annotationView;
}

// 当点击annotation view弹出的泡泡时，调用此接口
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;
{
    NSLog(@"paopaoclick");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
